home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1994 November / Cd Ware (Nro. 2) - Epimundo.iso / DOS / PG / PCLP41.ZIP / SIMPLE.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-04-18  |  4.1 KB  |  132 lines

  1. (*********************************************)
  2. (*                                           *)
  3. (*          SIMPLE.PAS      May 94           *)
  4. (*                                           *)
  5. (*  SIMPLE is provided as the simpliest      *)
  6. (*  possible terminal program using PCL4P    *)
  7. (*                                           *)
  8. (*  This program is donated to the Public    *)
  9. (*  Domain by MarshallSoft Computing, Inc.   *)
  10. (*  It is provided as an example of the use  *)
  11. (*  of the Personal Communications Library.  *)
  12. (*                                           *)
  13. (*********************************************)
  14.  
  15.  
  16. program simple;
  17. uses crt, PCL4P;
  18.  
  19. const
  20.    BaudCode = Baud2400;  (* Choose baud rate: Baud300 to Baud115200 *)
  21. var
  22.    Buffer  : array[0..1024] of Char;
  23.    RetCode : Integer;
  24.    Byte : Char;
  25.    i    : Integer;
  26.    Port : Integer;
  27.    ResetFlag : Boolean;
  28.  
  29. procedure SayError( Code : Integer );
  30. var
  31.    RetCode : Integer;
  32. begin
  33.    if Code < 0 then RetCode := SioError( Code )
  34.    else if (Code and (FramingError or ParityError or OverrunError)) <> 0 then
  35.       begin (* Port Error *)
  36.          if (Code and FramingError) <> 0 then writeln('Framing Error');
  37.          if (Code and ParityError)  <> 0 then writeln('Parity Error');
  38.          if (Code and OverrunError) <> 0 then writeln('Overrun Error')
  39.       end
  40. end;
  41.  
  42. procedure MyHalt( Code : Integer );
  43. var
  44.    RetCode : Integer;
  45. begin
  46.    if Code < 0 then SayError( Code );
  47.    if ResetFlag then RetCode := SioDone(Port);
  48.    writeln('*** HALTING ***');
  49.    Halt;
  50. end;
  51.  
  52. begin   (* main program *)
  53.    ResetFlag := FALSE;
  54.    (* fetch PORT # from command line *)
  55.    if ParamCount <> 1 then
  56.       begin
  57.          writeln('USAGE: "SIMPLE <port>" where port = 1 to 16');
  58.          halt;
  59.       end;
  60.    Val( ParamStr(1),Port, RetCode );
  61.    if RetCode <> 0 then
  62.       begin
  63.          writeln('Port must be 1 to 16');
  64.          Halt;
  65.       end;
  66.    (* COM1 = 0, COM2 = 1, etc. *)
  67.    Port := Port - 1;
  68.    if (Port<COM1) or (Port>COM16) then
  69.       begin
  70.          writeln('Port must be 1 to 16');
  71.          Halt
  72.       end;
  73.    (* setup 1K receive buffer *)
  74.    RetCode := SioRxBuf(Port, Ofs(Buffer), Seg(Buffer), Size1024);
  75.    if RetCode < 0 then MyHalt( RetCode );
  76.    (* reset port *)
  77.    RetCode := SioReset(Port,BaudCode);
  78.    (* if error then try one more time *)
  79.    if RetCode <> 0 then RetCode := SioReset(Port,BaudCode);
  80.    (* Was port reset ? *)
  81.    if RetCode <> 0 then
  82.      begin
  83.         writeln('Cannot reset COM',Port+1);
  84.         MyHalt( RetCode );
  85.      end;
  86.    (* Port successfully reset *)
  87.    writeln;
  88.    writeln('COM',1+Port,' @ 2400 Baud');
  89.    ResetFlag := TRUE;
  90.    (* specify parity, # stop bits, and word length for port *)
  91.    RetCode := SioParms(Port, NoParity, OneStopBit, WordLength8);
  92.    if RetCode < 0 then MyHalt( RetCode );
  93.    RetCode := SioRxFlush(Port);
  94.    if RetCode < 0 then MyHalt( RetCode );
  95.  
  96.    (* set DTR & RTS *)
  97.    RetCode := SioDTR(Port,SetPort);
  98.    RetCode := SioRTS(Port,SetPort);
  99.    (* begin terminal loop *)
  100.    writeln('Enter terminal loop ( Type ESC to exit )');
  101.    while TRUE do
  102.       begin
  103.          (* did user press Ctrl-BREAK ? *)
  104.          if SioBrkKey then
  105.             begin
  106.                writeln('User typed Ctl-BREAK');
  107.                RetCode := SioDone(Port);
  108.                Halt;
  109.             end;
  110.          (* anything incoming over serial port ? *)
  111.          RetCode := SioGetc(Port,0);
  112.          if RetCode < -1 then MyHalt( RetCode );
  113.          if RetCode > -1 then Write( chr(RetCode) );
  114.          (* has user pressed keyboard ? *)
  115.          if KeyPressed then
  116.             begin
  117.                (* read keyboard *)
  118.                Byte := ReadKey;
  119.                (* quit if user types ESC *)
  120.                if Byte = chr($1b) then
  121.                   begin
  122.                      writeln('User typed ESC');
  123.                      RetCode := SioDone(Port);
  124.                      Halt;
  125.                   end;
  126.                (* send out over serial line *)
  127.                RetCode := SioPutc(Port, Byte );
  128.                if RetCode < 0 then MyHalt( RetCode );
  129.             end
  130.       end
  131. end.
  132.